/**
 * 
 */
package org.softTank.BpelTranslator.Impl;

import javax.xml.namespace.QName;

import org.softTank.BcpModel.BcpActivity;
import org.softTank.BcpModel.BcpEmpty;
import org.softTank.BcpModel.BcpInvoke;
import org.softTank.BcpModel.BcpReceive;
import org.softTank.BcpModel.BcpReply;
import org.softTank.BpelModel.TActivity;
import org.softTank.BpelModel.TBoolean;
import org.softTank.BpelModel.TEmpty;
import org.softTank.BpelModel.TInvoke;
import org.softTank.BpelModel.TReceive;
import org.softTank.BpelModel.TReply;
import org.softTank.BpelTranslator.ActivityParser;

/**
 * @author Yuzone
 * 
 */
public class ActivityParserImpl implements ActivityParser {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.softTank.BpelTranslator.ActivityParser#parseActivity(org.softTank
	 * .BcpModel.BcpReceive)
	 */
	@Override
	public TActivity parseActivity(BcpActivity value) {
		if (value instanceof BcpReceive)
			return parseReceive((BcpReceive) value);
		if (value instanceof BcpReply)
			return parseReply((BcpReply) value);
		if (value instanceof BcpInvoke)
			return parseInvoke((BcpInvoke) value);
		if (value instanceof BcpEmpty)
			return parseEmpty((BcpEmpty) value);
		else
			return null;
	}

	private TActivity parseReceive(BcpReceive value) {
		TReceive receive = new TReceive();
		receive.setName(value.getName());
		receive.setOperation(value.getOperation());
		receive.setPartnerLink(value.getPartnerLink());
		receive.setCreateInstance(TBoolean.YES);
		if (value.getPortType() != null) {
			receive.setPortType(new QName(value.getPortType().getNs(), value
					.getPortType().getLocalPart()));
		}
		if (value.getVariable() != null) {
			receive.setVariable(value.getVariable().getName());
		}

		return receive;
	}

	private TActivity parseReply(BcpReply value) {
		TReply reply = new TReply();
		reply.setName(value.getName());
		reply.setOperation(value.getOperation());
		reply.setPartnerLink(value.getPartnerLink());
		if (value.getPortType() != null) {
			reply.setPortType(new QName(value.getPortType().getNs(), value
					.getPortType().getLocalPart()));
		}
		if (value.getVariable() != null) {
			reply.setVariable(value.getVariable().getName());
		}

		return reply;
	}

	private TActivity parseInvoke(BcpInvoke value) {
		TInvoke invoke = new TInvoke();
		invoke.setName(value.getName());
		if (value.getInputVariable() != null) {
			invoke.setInputVariable(value.getInputVariable().getName());
		}
		if (value.getOutputVariable() != null) {
			invoke.setOutputVariable(value.getOutputVariable().getName());
		}
		invoke.setOperation(value.getOperation());
		invoke.setPartnerLink(value.getPartnerLink());
		if (value.getPortType() != null) {
			invoke.setPortType(new QName(value.getPortType().getNs(), value
					.getPortType().getLocalPart()));
		}

		return invoke;
	}

	private TActivity parseEmpty(BcpEmpty value) {
		TEmpty empty = new TEmpty();
		empty.setName(value.getName());
		return empty;
	}

}
